package org.example.studentsystem_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Dorm;
import org.example.studentsystem_back.service.DormService;
import org.example.studentsystem_back.mapper.DormMapper;
import org.example.studentsystem_back.vo.dormMangeVo.DormIdVo;
import org.example.studentsystem_back.vo.dormMangeVo.DormVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 马鑫
* @description 针对表【dorm】的数据库操作Service实现
* @createDate 2024-07-12 10:51:55
*/
@Service
public class DormServiceImpl extends ServiceImpl<DormMapper, Dorm>
    implements DormService{
    @Resource
    private DormMapper dormMapper;

    @Override
    public CommonResponse<DormVo> findAll(int page, int limit, String name, int id)
    {
        Page<Dorm> page0 = new Page<>(page, limit);
        QueryWrapper<Dorm> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("area",name);
        if(id>0)
        {
            queryWrapper.eq("dorm_num",id);
        }
        queryWrapper.orderByAsc("area");
        queryWrapper.orderByAsc("dorm_num");
        List<Dorm> dormList=dormMapper.selectPage(page0,queryWrapper).getRecords();
        List<DormIdVo> dormIdVoList=new ArrayList<>();
        int i=page*10-9;
        for(Dorm dorm:dormList){
            DormIdVo dormIdVo=new DormIdVo();
            dormIdVo.setSuiId(i);
            dormIdVo.setDormIdVo(dorm);
            dormIdVoList.add(dormIdVo);
            i++;
        }
        DormVo dormVo=new DormVo();
        dormVo.setDormIdVoList(dormIdVoList);
        int total= Math.toIntExact(dormMapper.selectCount(queryWrapper));
        dormVo.setTotal(total);
        return CommonResponse.createForSuccess("查询成功",dormVo);
    }

    @Override
    public CommonResponse<Dorm> add(Dorm dorm)
    {
        dorm.setEmptyNum(dorm.getEnNum());
        dormMapper.insert(dorm);
        return CommonResponse.createForSuccess("添加成功",dorm);
    }

    @Override
    public CommonResponse<Dorm> update(Dorm dorm)
    {
        dormMapper.updateById(dorm);
        return CommonResponse.createForSuccess("更新成功",dorm);
    }

    @Override
    public CommonResponse<Dorm> delete(Dorm dorm)
    {
        dormMapper.deleteById(dorm);
        return CommonResponse.createForSuccess();
    }
}




